package com.lubase.metadata.service;

import com.lubase.core.extend.LookupColumnDataService;
import com.lubase.core.util.ClientMacro;
import com.lubase.orm.QueryOption;
import com.lubase.model.DbCollection;
import com.lubase.orm.service.AppHolderService;
import com.lubase.orm.service.DataAccess;
import com.lubase.orm.util.TableFilterWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lubase
 * @date 2021/09/08
 * @description
 */
@Service
public class GetAllowAccessDatabaseService implements LookupColumnDataService {

    @Override
    public String getColumnId() {
        return "1472601336217341952";
    }

    @Override
    public String getDescription() {
        return "获取自己有权限访问的数据库";
    }

    @Override
    public String getDisplayCol() {
        return "database_name";
    }

    @Override
    public String getExtendCol() {
        return "";
    }

    @Autowired
    DataAccess dataAccess;
    @Autowired
    AppHolderService appHolderService;

    @Override
    public DbCollection getColLookupData(ClientMacro clientMacro, String formData, QueryOption clientQuery) {
        QueryOption queryOption = new QueryOption("ms_database");
        queryOption.setFixField("id,database_name");
        TableFilterWrapper filterWrapper = TableFilterWrapper.and();
        filterWrapper.eq("db_manager", appHolderService.getUser().getCode());
        queryOption.setTableFilter(filterWrapper.build());
        return dataAccess.query(queryOption);
    }
}
